<?php
include_once "exportheader.inc";

$submit = strtolower($_POST['submit']);
$conf_id = intval($_POST['conf_id']);


switch (TRUE) {
case ($submit == 'download csv'):
	csv_user_report($conf_id);
	break;
case ($submit == 'csv for vendors'):
	csv_user_report_vendors($conf_id);
	break;
case ($submit == 'name badges pdf'):
	//debug_output($submit);
	pdf_name_badges($conf_id);
	break;
default:
	redirect_header(XOOPS_URL."/modules/confreg/admin/admenu5.php",5, "error in data. Redirecting to reports menu.");
	break;
}

exit;

function csv_user_report($conf_id) {
	global $xoopsDB;

	// charcter values
	$nl = "\r\n";
	$q = '"';
	$qt = $q . "\t";
	$qtq = $q . "\t" . $q;
	
	$conf_id = intval($conf_id);
	// conference data
	$cHdl =& $xoopsDB->query("SELECT title,confirm_offset" .
							 " FROM " . $xoopsDB->prefix("confreg_conferences") .
							 ' WHERE conf_id = ' . $conf_id );
	$cData = $xoopsDB->fetchArray($cHdl);

	$content = $q . $cData['title'].' User Report"' . $nl;
	// column headers
	$content .= $q . 'Confirmation' . $qtq . 'Company' . $qtq . 'First Name' . $qtq . 'Last Name' .
				$qtq . 'TMC Number' . $qtq . 'Email' . $qtq . 'Days Attending' .
				$qtq . 'Extras' . $qtq . 'Cost' . $qtq . 'Paid' . $q . $nl;

	$users = get_all_users($conf_id);

	foreach ($users as $uRec) {
		$content .= $q . $cData['confirm_offset'] . $uRec['group_id'] .
					$qtq . $uRec['company'] . $qtq . $uRec['first_name'] .
					$qtq . $uRec['last_name'] . $qtq . $uRec['tmc_num'] .
					$qtq . $uRec['email'] . $qtq . $uRec['days_attending'] .
					$qtq . $uRec['extras'] . $qtq . $uRec['cost'] . $qtq . $uRec['paid'] .
					$qt . $nl;
	}
	send_file("csv_user_report.txt", $content . "\r\n");
}

function csv_user_report_vendors($conf_id) {
	global $xoopsDB;

	// charcter values
	$nl = "\r\n";
	$q = '"';
	$qt = $q . "\t";
	$qtq = $q . "\t" . $q;
	
	$conf_id = intval($conf_id);
	// conference data
	$cHdl =& $xoopsDB->query("SELECT title,confirm_offset" .
							 " FROM " . $xoopsDB->prefix("confreg_conferences") .
							 ' WHERE conf_id = ' . $conf_id );
	$cData = $xoopsDB->fetchArray($cHdl);

	$content = $q . $cData['title'].' User Report"' . $nl;
	// column headers
	$content .= $q . 'Company' . $qtq . 'First Name' . $qtq . 'Last Name' .
				$qtq . 'Email' . $q . $nl;

	$users = get_all_users($conf_id);

	foreach ($users as $uRec) {
		$content .= $q . $uRec['company'] . $qtq . $uRec['first_name'] .
					$qtq . $uRec['last_name'] . $qtq;
		if (stripos($uRec['extras'], "provide e-mail to vendors") !== false) {
			$content .= $uRec['email'] . $q . $nl;
		} else {
			$content .= $q . $nl;
		}
	}
	send_file("csv_user_report_for_vendors.txt", $content . "\r\n");
}

function debug_output($inp) {
	echo "<html><head><title>debug</title></head>";
	echo "<body>Error<p><code>";
	var_dump ($inp);
	echo "</code></body></html>";
}


function pdf_name_badges($conf_id) {
	global $xoopsDB;
	require_once('../class/tcpdf/config/lang/eng.php');
	require_once('../class/tcpdf/tcpdf.php');

	$nl = "\r\n";

	// assumes name badges are printed on letter size paper with a top/bottom margin of 1"
	// no side margins and a 3" tall badge with a .9" header
	// conference data
	$cHdl =& $xoopsDB->query("SELECT title,confirm_offset" .
							 " FROM " . $xoopsDB->prefix("confreg_conferences") .
							 ' WHERE conf_id = ' . $conf_id );
	$cData = $xoopsDB->fetchArray($cHdl);
	$users = get_all_users($conf_id);
	// create new PDF document
	$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, 'LETTER', true, 'UTF-8', false); 
	
	// set document information
	$pdf->SetCreator(PDF_CREATOR);
	$pdf->SetAuthor('Mid-Continent MicroStation Community');
	$pdf->SetTitle($conf['title']);
	$pdf->SetSubject($conf['title']);
	$pdf->SetKeywords(str_replace(' ', ',', $conf['title']));

	// no header or footer
	$pdf->setPrintHeader(false);
	$pdf->setPrintFooter(false); 	// set default header data
	// set default monospaced font
	$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

	// margins in mm. 25.4 mm = 1"
	//              (LEFT, TOP, RIGHT)
	$pdf->SetMargins(  8,  48,    4);

	//set auto page breaks
	//            PDF_MARGIN_BOTTOM
	$pdf->SetAutoPageBreak(FALSE, 0);

	//set image scale factor
	$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); 

	//set some language-dependent strings
	$pdf->setLanguageArray($l); 

	// ---------------------------------------------------------

	// set font
	$pdf->SetFont('helvetica', '', 12);

	$pdf->AddPage();
	$pdf->lastPage();
	
	$page = '<table width=8in>'.$nl;
	$count = 1;
	foreach ($users as $uRec) {
		// odd number cells start a new row
		if ($count % 2 != 0) {
			$page .= '<tr>';
		}
		// add table cell and logo
		$page .= '<td width=4in style="padding:100em;"><img src="../../../site_images/logo_banner.gif" align="left" border="0" width="91" height="20"/>';
		// if going to power play add indicator in red, on right of cell
		if (stripos($uRec['extras'],"powerplay") !== false) { 
			$page .= str_repeat('&nbsp;',40) . '<span style="font-size:small; color:red;">PowerPlay</span>';
		}
		$page .= $nl . '<div style="text-align:center; font-size: xx-large;">' . $uRec['first_name'] . ' ' . $uRec['last_name'] . '</div>';
		$page .= $nl . '<div style="width:2in; text-align:center; font-size: x-large;">' . $uRec['company'] . '</div>';
		if ((stripos($uRec['tmc_num'],'speaker') !== false) || (stripos($uRec['tmc_num'],'exhibitor') !== false) || (stripos($uRec['tmc_num'],'committee') !== false)) {
			$page .= $nl . '<div style="text-align:center; font-size: medium;">'. ucfirst($uRec['tmc_num']) . '</div>';
		}
		$page .= $nl . '<div style="text-align:center; font-size: small;">Days Attending:<br />' . str_replace(';', '<br />', substr($uRec['days_attending'], 0, -2)) . '</div>';
		$page .= str_repeat($nl . '<br />',6) . '</td>';
		// even numbers end a row
		if ($count % 2 == 0) {
			$page .= '</tr>';
		}
		// every 6 names we start a new page & table
		if ($count % 6 == 0) {
			// end the table, start a new one for next pages
			$page .= '</table>';
			$page .= '<tcpdf method="AddPage" />';
			$page .= '<table width=8in>' . $nl;
		}
		$count++;
	}
	$pdf->writeHTML($page, true, 0, true, 0);

	$pdf->lastPage();

	// $pdf->Output($cData['title']. ' Name Badges.pdf', 'I');
	$pdf->Output('Name_Badges.pdf', 'I');
}

function pdf_name_badges_new($conf_id) {
	global $xoopsDB;
	require_once('../class/tcpdf/config/lang/eng.php');
	require_once('../class/tcpdf/tcpdf.php');

	// assumes name badges are printed on letter size paper with a top/bottom margin of 1"
	// no side margins and a 3" tall badge with a .9" header
	// conference data
	$cHdl =& $xoopsDB->query("SELECT title,confirm_offset" .
							 " FROM " . $xoopsDB->prefix("confreg_conferences") .
							 ' WHERE conf_id = ' . $conf_id );
	$cData = $xoopsDB->fetchArray($cHdl);
	$users = get_all_users($conf_id);
	// create new PDF document
	$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, 'LETTER', true, 'UTF-8', false); 
	
	// set document information
	$pdf->SetCreator(PDF_CREATOR);
	$pdf->SetAuthor('Mid-Continent MicroStation Community');
	$pdf->SetTitle($conf['title']);
	$pdf->SetSubject($conf['title']);
	$pdf->SetKeywords(str_replace(' ', ',', $conf['title']));

	// no header or footer
	$pdf->setPrintHeader(false);
	$pdf->setPrintFooter(false); 	// set default header data
	// set default monospaced font
	$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

	// margins in mm. 25.4 mm = 1"
	//              (LEFT, TOP, RIGHT)
	$pdf->SetMargins(  0,  26,    0);

	//set auto page breaks
	//            PDF_MARGIN_BOTTOM
	$pdf->SetAutoPageBreak(TRUE, 26);

	//set image scale factor
	$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); 

	//set some language-dependent strings
	$pdf->setLanguageArray($l); 

	// ---------------------------------------------------------

	// set font
	$pdf->SetFont('helvetica', '', 12);

	$pdf->AddPage();
	$pdf->lastPage();
	
	$page = '<table width=100% height=9in>';
	$count = 1;
	foreach ($users as $uRec) {
		// odd number cells start a new row
		if ($count % 2 != 0) {
			$page .= '<tr>';
		}
		// add table cell and logo
		$page .= '<td width=4.25in height=3in style="padding: 10em;"><img src="../../../site_images/logo_banner.gif" align="left" border="0" width="82" height="18"/>';
		// if going to power play add indicator in red, on right of cell
		if (stripos($uRec['extras'],"powerplay") !== false) { 
			$page .= str_repeat('&nbsp;',50) . '<span style="font-size:small; color:red;">PowerPlay</span>';
		}
		$page .= '<div style="text-align:center; font-size: xx-large;">' . $uRec['first_name'] . ' ' . $uRec['last_name'] . '</div>';
		$page .= '<div style="text-align:center; font-size: x-large;">' . $uRec['company'] . '</div>';
		if ((stripos($uRec['tmc_num'],'speaker') !== false) || (stripos($uRec['tmc_num'],'exhibitor') !== false) || (stripos($uRec['tmc_num'],'committee') !== false)) {
			$page .= '<div style="text-align:center; font-size: medium;">'. ucfirst($uRec['tmc_num']) . '</div>';
		}
		$page .= '<div style="text-align:center; font-size: small;">Days Attending:<br />' . str_replace(';', '<br />', substr($uRec['days_attending'], 0, -2)) . '</div>';
		$page .= str_repeat('<br />',5) . '</td>';
		// even numbers end a row
		if ($count % 2 == 0) {
			$page .= '</tr>';
		}
		// every 6 names we start a new page
		if ($count % 6 == 0) {
			$page .= '<tcpdf method="AddPage" />';
		}
		$count++;
	}
	$pdf->writeHTML($page, true, 0, true, 0);

	$pdf->lastPage();

	// $pdf->Output($cData['title']. ' Name Badges.pdf', 'I');
	$pdf->Output('Name_Badges.pdf', 'I');
}
?>
